//
//  UCCustomComponent.m
//  SDK开发版本
//
//  Created by 北京国泰北斗科技有限公司 on 2017/2/7.
//  Copyright © 2017年 北京国泰北斗科技有限公司. All rights reserved.
//

#import "UCCustomComponent.h"
#import "UCMapView.h"
#import "UCMarker.h"
#import <GoogleMaps/GoogleMaps.h>
#import "UCMapView+GSExtension.h"
#import "UCMutablePath.h"

@interface UCCustomComponent()
@property(nonatomic,strong)UCMapView *mapView;
@end

@implementation UCCustomComponent

- (NSMutableArray *)markers{
    if (!_markers) {
        _markers = [NSMutableArray array];
    }
    return _markers;
}

- (UCMutablePath *)mutablePath{
    if (!_mutablePath) {
        _mutablePath = [UCMutablePath path];
    }
    return _mutablePath;
}

+ (instancetype)componentWithMapView:(UCMapView *)mapView{
    
    return [[self alloc] initWithMapView:mapView];
    
}

- (instancetype)initWithMapView:(UCMapView *)mapView{
    
    self.mapView = mapView;
    
    return self;
}

///根据传入的点获取区域
- (void)addCoordToComponent:(CLLocationCoordinate2D)position{
    
    [self.mutablePath addCoordinate:position];
    
    UCMarker *marker = [UCMarker markerWithPosition:position];
    marker.icon = [self.mapView generateClusterIconWithCount:self.markers.count+1];
    //    marker.groundAnchor = CGPointMake(0.5, 0.5);
    marker.zIndex = 5;
    marker.draggable = YES;
    marker.mapView = self.mapView;
    [self.markers addObject:marker];
}

///重置选择区域
- (void)resetChoseArea{
    
    for (UCMarker *marker in self.markers) {
        marker.mapView = nil;
    }
    [self.markers removeAllObjects];
    
    [self.mutablePath removeAllCoordinates];
}

- (void)changeCoordWhenDragMarker:(UCMarker *)marker{
    
    for (int i = 0; i < self.markers.count; i++) {
        UCMarker *gMarker = self.markers[i];
        if ([gMarker valueForKey:@"marker"] == [marker valueForKey:@"marker"]) {
            
            GMSMarker *googleMar = [marker valueForKey:@"marker"];
            
            CLLocationCoordinate2D gooleCoord = googleMar.position;
            
            //            CLLocationCoordinate2D myCoord = gMarker.coordinate;
            
            [gMarker setCoordinate:gooleCoord];
            
            [self.mutablePath replaceCoordinateAtIndex:i withCoordinate:marker.coordinate]; 
        }
    }
}
@end
